﻿using Acme.BookStore.Authors;
using Acme.BookStore.Bass;
using Acme.BookStore.Books;
using Acme.BookStore.Navigates;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using SqlSugar;
using System;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore
{
    public class BookStoreTestDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        private static readonly Guid User1Id = Guid.Parse("41951813-5CF9-4204-8B18-CD765DBCBC9B");
        private static readonly Guid User2Id = Guid.Parse("2DAB4460-C21B-4925-BF41-A52750A9B999");

        private readonly IRepository<Book> _bookRepository;
        private readonly IAuthorRepository _authorRepository;

        public BookStoreTestDataSeedContributor(IRepository<Book> bookRepository, IAuthorRepository authorRepository)
        {
            _bookRepository = bookRepository;
            _authorRepository = authorRepository;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            await _authorRepository.InsertAsync(new Author(
                User1Id,
                "Author1",
                DateTime.Today,
                ""
            ));
            await _authorRepository.InsertAsync(new Author(
              User2Id,
              "Author2",
              DateTime.Today,
              ""
          ));
            await _bookRepository.InsertAsync(
                new Book
                {
                    Name = "Test Book one",
                    Price = 11,
                    AuthorId = User1Id,
                });

            await _bookRepository.InsertAsync(
                new Book
                {
                    Name = "Test Book two",
                    Price = 22,
                    AuthorId = User2Id,
                });

            await _bookRepository.InsertAsync(
                new Book
                {
                    Name = "Test Book three",
                    Price = 33,
                    AuthorId = User2Id,
                });

            await _bookRepository.InsertAsync(
                new Book
                {
                    Name = "1984",
                    Price = 44,
                    AuthorId = User1Id
                });
        }
    }

    public class BookStoreTestDataSeedSqlSugar : ITransientDependency
    {
        public ILogger<BookStoreTestDataSeedSqlSugar> Logger { get; set; }

        private readonly SqlSugarScope db; // 核心对象：拥有完整的SqlSugar全部功能 单例

        public BookStoreTestDataSeedSqlSugar(SqlSugarScope Db)
        {
            db = Db;
            Logger = NullLogger<BookStoreTestDataSeedSqlSugar>.Instance;
        }

        public async Task SeedSqlSugarDataAsync()
        {
            if (!db.Queryable<Bas>().Any())
            {
                await db.Insertable(new Bas() { Code = "testCode1", Name = "testBasName1" }).ExecuteCommandAsync();
                await db.Insertable(new Bas() { Code = "111", Name = "111" }).ExecuteCommandAsync();
            }
            if (!db.Queryable<Room>().Any())
            {
                await db.Insertable(new Room() { RoomId = 1, RoomName = "test北大001室", SchoolId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new Room() { RoomId = 2, RoomName = "test北大002室", SchoolId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new Room() { RoomId = 3, RoomName = "test北大003室", SchoolId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new Room() { RoomId = 4, RoomName = "test清华001厅", SchoolId = 2 }).ExecuteCommandAsync();
                await db.Insertable(new Room() { RoomId = 5, RoomName = "test清华002厅", SchoolId = 2 }).ExecuteCommandAsync();
                await db.Insertable(new Room() { RoomId = 6, RoomName = "test清华003厅", SchoolId = 2 }).ExecuteCommandAsync();
            }
            if (!db.Queryable<School>().Any())
            {
                await db.Insertable(new School() { SchoolId = 1, SchoolName = "test北大" }).ExecuteCommandAsync();
                await db.Insertable(new School() { SchoolId = 2, SchoolName = "test清华" }).ExecuteCommandAsync();
            }
            if (!db.Queryable<Student>().Any())
            {
                await db.Insertable(new Student() { StudentId = 1, SchoolId = 1, Name = "test北大jack" }).ExecuteCommandAsync();
                await db.Insertable(new Student() { StudentId = 2, SchoolId = 1, Name = "test北大tom" }).ExecuteCommandAsync();
                await db.Insertable(new Student() { StudentId = 3, SchoolId = 2, Name = "test清华jack" }).ExecuteCommandAsync();
                await db.Insertable(new Student() { StudentId = 4, SchoolId = 2, Name = "test清华tom" }).ExecuteCommandAsync();
            }
            if (!db.Queryable<Teacher>().Any())
            {
                await db.Insertable(new Teacher() { SchoolId = 1, Id = 1, Name = "test北大老师01" }).ExecuteCommandAsync();
                await db.Insertable(new Teacher() { SchoolId = 1, Id = 2, Name = "test北大老师02" }).ExecuteCommandAsync();
                await db.Insertable(new Teacher() { SchoolId = 2, Id = 3, Name = "test清华老师01" }).ExecuteCommandAsync();
                await db.Insertable(new Teacher() { SchoolId = 2, Id = 4, Name = "test清华老师02" }).ExecuteCommandAsync();
            }
            if (!db.Queryable<StuBook>().Any())
            {
                await db.Insertable(new StuBook() { BookId = 1, Name = "testbook1", StudentId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 1, Name = "testbook1", StudentId = 2 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 1, Name = "testbook1", StudentId = 3 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 1, Name = "testbook1", StudentId = 4 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 2, Name = "testbook2", StudentId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 2, Name = "testbook2", StudentId = 2 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 2, Name = "testbook2", StudentId = 3 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 2, Name = "testbook2", StudentId = 4 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 3, Name = "testbook3", StudentId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 3, Name = "testbook3", StudentId = 2 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 3, Name = "testbook3", StudentId = 3 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 4, Name = "testbook4", StudentId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 5, Name = "testbook5", StudentId = 1 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 5, Name = "testbook5", StudentId = 2 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 5, Name = "testbook5", StudentId = 3 }).ExecuteCommandAsync();
                await db.Insertable(new StuBook() { BookId = 5, Name = "testbook5", StudentId = 4 }).ExecuteCommandAsync();
            }
            Logger.LogInformation($"Successfully completed seed SqlSugarTestData.");
        }
    }
}